package org.bluedream.comm.utils.excel;

import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.ReflexUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName ExcelUtil
 * @Description TODO
 * @Author foxsand
 * @Data 2021-08-30 8:41
 * @Version
 */
public class ExcelUtil implements Serializable {
    private static final long serialVersionUID = 5952245122807291623L;

    public static List<ExcelAnnotationAnalysis> getAnnotationAnalyses(Class<?> clazz , ExcelType type){
        List<ExcelAnnotationAnalysis> annotationAnalyses = new ArrayList<>();
        List<Field> fieldList = ReflexUtil.getObjectFields(clazz);
        for (Field f1:fieldList
        ) {
            FieldType fieldType = f1.getAnnotation(FieldType.class);
            if (EmptyUtil.isNoEmpty(fieldType) && (fieldType.excelType() == type || fieldType.excelType() == ExcelType.ALL)){
                ExcelAnnotationAnalysis annotationAnalysis;
                if (type == ExcelType.EXPORT){
                    annotationAnalysis   = new ExcelAnnotationAnalysis(f1.getName() , f1.getType() , fieldType.excelType() , fieldType.excelFieldName() , fieldType.fieldSort());
                }else if (type == ExcelType.IMPORT){
                    annotationAnalysis = new ExcelAnnotationAnalysis(f1.getName() , f1.getType() , fieldType.excelType() , fieldType.excelFieldName() , fieldType.isRequired());
                }else {
                    throw new ExcelException("ExcelException:不可预知的动作，请确认具体操作：导入 or 导出！");
                }
                annotationAnalyses.add(annotationAnalysis);
            }
        }
        // list 排序
        Collections.sort(annotationAnalyses);
        return annotationAnalyses;
    }

    /**
     * 文件导出
     * @param response
     * @param dataList
     * @param clazz
     * @param args 定义一组文件属性，允许为空，最大允许长度：3个参数。0：fileName，1：sheetName，2：顶端标题，默认没有标题行
     */
    public static void export4List(HttpServletResponse response, List<?> dataList , Class<?> clazz , String... args){
        new ExcelExport(response , dataList , clazz , args);
    }

    /**
     * MultipartFile方式导入文件
     * @param file
     * @param sheetIndexOrName
     * @param clazz
     * @param headerRowNum
     * @param <E>
     * @return
     */
    public static <E>List<E> import2List(MultipartFile file , Object sheetIndexOrName , Class<E> clazz , int headerRowNum){
        ExcelImport excelImport = new ExcelImport();
        return excelImport.readExcel2List(file , sheetIndexOrName , clazz , headerRowNum);
    }

    public static List<String> import2List(MultipartFile file , Object sheetIndexOrName , int headerRowNum){
        ExcelImport excelImport = new ExcelImport();
        return excelImport.readExcel2List(file , sheetIndexOrName , headerRowNum);
    }

    /**
     * File方式导入文件
     * @param file
     * @param sheetIndexOrName
     * @param clazz
     * @param headerRowNum
     * @param <E>
     * @return
     */
    public static <E>List<E> import2List(File file , Object sheetIndexOrName , Class<E> clazz , int headerRowNum){
        ExcelImport excelImport = new ExcelImport();
        return excelImport.readExcel2List(file , sheetIndexOrName , clazz , headerRowNum);
    }
}
